package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.common.UserManger;
import gl.view.pulltofresh.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 消息盒子
 * 
 * @author geliang
 * 
 */
public class UserListFragment extends BaseListFragmet implements
		OnClickListener {
	protected final String ERR = "获取失败";
	private TextView empty;
	private ListView list;
	protected String NOTFOUND = "目前没有什么内容";
	private Drawable drawable;
	private EditText et_msg;
	private View btn_send;
	private RadioGroup mRadioGroup;
	private ViewPager pager;

	// private PullToRefreshView pullToRefreshView1;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		// pullToRefreshView1 =
		// (PullToRefreshView)findViewById(R.id.pullToRefreshView1);
		// pullToRefreshView1.onHeaderRefreshComplete();
		list = (ListView) findViewById(android.R.id.list);

		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public UserListFragment() {
	}

	private static final String TYPE_HOME = "2";
	private int maxID = 0;
	String urls[];
	private int messageType;
	private int sex;
	public static final String key = "messageType";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container, false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_search_btn_civil_n);
		urls = new String[] {
				"GetHomeViewer?userid=" + UserManger.getInstance().userid
						+ "&zw=%s&type=%s&maxid=%d",
				"GetRecommends?userid=" + UserManger.getInstance().userid
						+ "&sex=%s&type=%s&maxid=%d",
				"GetCareer?userid=" + UserManger.getInstance().userid
						+ "&sex=%s&type=%s&maxid=%d", };
		bindViews();
		SEX = UserManger.getInstance().userinfo.getGender();
		SEX_RECOMMND = (SEX == 1 ? 2 : 1);

		sex = SEX;
		switch (messageType) {
		case 0:// 推荐会员

			break;
		case 1:
			sex = SEX_RECOMMND;
			break;
		case 2:
			break;
		case 3:
			break;

		default:
			break;
		}
		messageType = Integer.parseInt(getArguments().getString(KEY_USER_ID));
		initdata();
		return rootView;
	}

	private static int SEX = 2;
	private static int SEX_RECOMMND = 1;

	private void initdata() {

		initAbsListView(getListView(), API.URL + urls[messageType],
				new Object[] { sex, TYPE_HOME, maxID },
				R.layout.item_list_image, new String[] { "username", "avatar",
						BYQUserInfo.KEY_SHENFEN, "monolog",
						BYQUserInfo.KEY_GROUP, BYQUserInfo.KEY_VIP_UNIT },
				new int[] { R.id.tv_nick, R.id.iv_avator, R.id.age,
						R.id.tv_jianjie, R.id.iv_vip, R.id.imageView1,
						R.id.iv_fav, R.id.iv_sendmsg });
	}

	@Override
	protected String preFormatJsonData(String t) {
		try {
			JSONArray ja = new JSONObject(t).getJSONArray("data");
			String maxPageID = "userid";
			if (messageType == 0) {
				maxPageID = "viewid";
			}
			for (int i = 0; i < ja.length(); i++) {
				//
				int id = ja.getJSONObject(i).getInt(maxPageID);
				// maxID =id>maxID?id:maxID;//取最大
				if (maxID == 0) {
					maxID = id;
				}
				else {
					maxID = id < maxID ? id : maxID;// 取最小
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BYQUserInfo.preFormatShenFen(t);
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		super.onHeaderRefresh(view);
		initList();

	}

	public void initList() {
		maxID = 0;
		initdata();
	}

	@Override
	protected boolean getLoadMoreAble() {
		return true;
	}

	protected boolean getRefreAble() {
		return true;
	}

	@Override
	public void onBGARefreshLayoutBeginRefreshing() {
		initList();
	}

	@Override
	public void onBGARefreshLayoutBeginLoadingMore() {
		appendAbsListView(getListView(), API.URL + urls[messageType],
				new Object[] { sex, TYPE_HOME, maxID });
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		setPullListviewAble(true);
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		appendAbsListView(getListView(), API.URL + urls[messageType],
				new Object[] { sex, TYPE_HOME, maxID });
	}
}